package com.msb.mall.order.web;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.msb.common.exception.NoStockExecption;
import com.msb.mall.order.config.AlipayTemplate;
import com.msb.mall.order.service.OrderService;
import com.msb.mall.order.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Auther: xixifeng
 * @Date: 2022/11/13 - 11 - 13 - 18:57
 * @Description: com.msb.mall.order.web
 * @version: 1.0
 */

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    AlipayTemplate alipayTemplate;


    /**
     * 获取订单确认页中需要获取的相关信息
     * @return
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVo confirmVo =orderService.comfirmOrder();

        model.addAttribute("confirmVo",confirmVo);
        return "confirm";
    }


    /**
     * 提交订单
     * @param vo
     * @return
     */
    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO vo, Model model, RedirectAttributes redirectAttributes){
        OrderResponseVO responseVO =null;
        Integer code=0;
        try {
            responseVO = orderService.orderSubmit(vo);
            code=responseVO.getCode();
        } catch (NoStockExecption execption) {
            code=2;
        }

        if (code==0){
         // 表示下单操作成功
            model.addAttribute("orderResponseVO", responseVO);
        return "pay";
        }else {
            String msg="订单失败";
            if (code == 1) {
                msg=msg+"订单重复提交";
            }else if (code==2){
                msg=msg+"订单锁定失败";
            }
            redirectAttributes.addFlashAttribute("msg", msg);
            // 表示下单操作失败，重定向到详情页面
            return "redirect:http://order.msb.com:8030/toTrade";
        }

    }

    /**
     * 支付完了之后就更新相关状态
     * @param orderSn
     * @param out_trade_no
     * @return
     */
    @GetMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSn",required = false) String orderSn,
                           @RequestParam(value = "out_trade_no",required = false) String out_trade_no){
        // TODO 完成相关的支付操作
        System.out.println("orderSn = " + orderSn);
        if(StringUtils.isNotBlank(orderSn)){
            orderService.handleOrderComplete(orderSn);

        }else{
            //orderService.updateOrderStatus(out_trade_no,OrderConstant.OrderStateEnum.TO_SEND_GOODS.getCode());
            orderService.handleOrderComplete(out_trade_no);
        }
        return "list";
    }


        /**
         * 获取订单相关信息
         * 然后跳转到支付页面
         * @param orderSn
         * @return
         * **/
         @GetMapping(value = "/payOrder",produces = "text/html")
         @ResponseBody
            public String payOrder(@RequestParam("orderSn") String orderSn){

            PayVo payVo =orderService.getOrderPay(orderSn);
             String pay = alipayTemplate.pay(payVo);

             return pay;
            }
}
